namespace Yaaf.WP7.NativeAccess.IO
{
    using System;

    public class PathInfo
    {
        public static PathInformation GetPathInformation(string path)
        {
            return FileSystemAccess.GetPathInformation(path);
        }



        private static string rootIsolatedDir;

        private static string rootApplicationDir;

        public static string RootIsolatedStorage
        {
            get
            {
                if (rootIsolatedDir == null)
                {
                    rootIsolatedDir = System.IO.Path.Combine(System.IO.Path.Combine(@"\Applications\Data", DeviceInfo.ApplicationId.ToString("D")), @"Data\IsolatedStore");
                }

                return rootIsolatedDir;
            }
        }

        public static string RootApplicationDir
        {
            get
            {
                if (rootApplicationDir == null)
                {
                    rootApplicationDir = System.IO.Path.Combine(System.IO.Path.Combine(@"\Applications\Install", DeviceInfo.ApplicationId.ToString("D")), @"Install");
                }

                return rootApplicationDir;
            }
        }

        public static string ToGeneral(string isolatedPath)
        {
            //var filesAppField = typeof(IsolatedStorageFile).GetField("m_AppFilesPath", BindingFlags.NonPublic | BindingFlags.Instance);
            //var rootIsolatedDir = (string)filesAppField.GetValue(file);
            return System.IO.Path.Combine(RootIsolatedStorage, isolatedPath);
        }

        public static bool IsInIsolatedStorage(string path)
        {
            return path.StartsWith(RootIsolatedStorage);
        }
        
        public static string ToIsolatedStoragePath(string completePath)
        {
            if (!completePath.StartsWith(RootIsolatedStorage))
            {
                throw new ArgumentException("this is no IsolatedStoragePath!");
            }

            var tempPath = completePath.Substring(RootIsolatedStorage.Length);
            return tempPath.StartsWith("\\") ? tempPath.Substring(1) : tempPath;
        }
    }
}